package com.aliyun.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.aliyun.service.NikeService;


@Controller 
public class NikeController {
	private final Log log = LogFactory.getLog(NikeController.class);
    
    @Resource
	private NikeService nikeService;
    
    @ResponseBody
	@RequestMapping(value="/insertNikeOrderDetail",produces = {"application/text;charset=UTF-8"}) 
	public String insertNikeOrderDetail(@RequestBody String param)
	{
		System.out.println(param);
		String[] paramStrs = param.split("&");
		if(paramStrs.length>0){
			Map<String,String> map = new HashMap<String,String>();
			for(String temp : paramStrs ){
				String[] temps = temp.split("=");
				if("displayName".equals(temps[0])
						|| "colorDescription".equals(temps[0])
						|| "name".equals(temps[0])
						|| "status".equals(temps[0])
						|| "submittedDate".equals(temps[0])){
					try {
						String displayName = URLDecoder.decode(temps[1],"utf-8");
						map.put(temps[0], displayName);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					map.put(temps[0], temps[1]);
				}
			}
			try {
				nikeService.insertNikeOrderDetail(map);
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "1";
	}
    @ResponseBody
	@RequestMapping(value="/insertNikeOrder",produces = {"application/text;charset=UTF-8"}) 
	public String insertNikeOrder(@RequestBody String param)
	{
		System.out.println(param);
		String[] paramStrs = param.split("&");
		if(paramStrs.length>0){
			Map<String,String> map = new HashMap<String,String>();
			for(String temp : paramStrs ){
				String[] temps = temp.split("=");
				if("payAliUrl".equals(temps[0])){
					try {
						String payAliUrl = URLDecoder.decode(temps[1],"utf-8");
						map.put("payAliUrl", payAliUrl);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else if("username".equals(temps[0])){
					try {
						String username = URLDecoder.decode(temps[1],"utf-8");
						map.put("username", username);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					map.put(temps[0], temps[1]);
				}
			}
			try {
				nikeService.insertNikeOrder(map);
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "1";
	}
    @ResponseBody
	@RequestMapping(value="/getOneNikeOrder",method = RequestMethod.GET,produces = {"application/text;charset=UTF-8"}) 
	public String getNikeOrder(HttpServletRequest request)
	{	
    	String returnStr = "";
    	try {
			Map<String,Object> orderMap = nikeService.getOneNikeOrder();
			System.out.println(JSON.toJSONString(orderMap));
			returnStr=JSON.toJSONString(orderMap);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnStr;
	}
    @ResponseBody
	@RequestMapping(value="/getAllNikeOrder",method = RequestMethod.POST,produces = {"application/text;charset=UTF-8"}) 
	public String getAllNikeOrder(@RequestBody String param)
	{	
    	System.out.println(param);
    	String returnstr = "{}";
    	String[] dates = param.split("&");
    	String startDate = dates[0].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("startDate", startDate);
    	try {
			List<Map<String,Object>> orderList = nikeService.getAllNikeOrder(map);
			if(orderList!=null && orderList.size()>0){
				JSONArray jArr = JSONArray.fromObject(orderList);
				returnstr = jArr.toString();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnstr;
	}
    @ResponseBody
	@RequestMapping(value="/getAllNikeOrderDetail",method = RequestMethod.POST,produces = {"application/text;charset=UTF-8"}) 
	public String getAllNikeOrderDetail(@RequestBody String param)
	{	
    	System.out.println(param);
    	String returnstr = "{}";
    	String[] dates = param.split("&");
    	String startDate = dates[0].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("startDate", startDate);
    	try {
			List<Map<String,Object>> orderList = nikeService.getAllNikeOrderDetail(map);
			if(orderList!=null && orderList.size()>0){
				JSONArray jArr = JSONArray.fromObject(orderList);
				returnstr = jArr.toString();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnstr;
	}
}
